import pandas as pd
import pinyin
from tqdm import tqdm

outfile = "name_gender_eng.csv"
os.remove(outfile)
indf = pd.read_csv("name_gender.csv", header=0)
nrows = indf.shape[0]

for index, row in tqdm(indf.iterrows()):
    
    print(f"Processing {index+1}/{nrows} ({(index+1)/nrows:.0%})", end='\r')
    
    name_ch = row["name"]   
    if pd.isnull(name_ch):
        print(f"Found null on row {index}")
        continue
    gender_ch = row["gender"]
    
    gender_eng = "unknown"
    if gender_ch=="男":
        gender_eng = "male"
    if gender_ch == "女":
        gender_eng = "female"
        
    #print(f"Processing {name_ch}")
    name_pinyin = pinyin.get(name_ch)
    name_pinyin_strip_spaced = pinyin.get(name_ch, format="strip", delimiter=" ")
    name_pinyin_strip_united = pinyin.get(name_ch, format="strip", delimiter="")
    
    indf.loc[index, "gender_eng"] = gender_eng
    indf.loc[index, "name_pinyin"] = name_pinyin
    indf.loc[index, "name_pinyin_strip_spaced"] = name_pinyin_strip_spaced
    indf.loc[index, "name_pinyin_strip_united"] = name_pinyin_strip_united
    
indf.to_csv(outfile, index=False)
    

